package com.common.widget;

import com.common.utils.StarAppUtils;
import com.starappallcommon.R;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

/**
 * 通用信息提示框
 * 
 * <br>
 * Created 2014-6-6 下午5:25:26
 * 
 * @version
 * @author linsj
 * 
 * @see
 */
public class CommonSendMsgCodeDlg extends Dialog {
    /** 上下文 */
    private Context mContext;

    /** 标题 */
    private TextView mtvTitle;

    /** 将会发送到 */
    private TextView mtvWillSendTo;

    /** 提示信息文字textview */
    private TextView mtvTip;
    /** 提示信息文字 */
    private String mstrTip;
    /** 提示信息标题文字 */
    private String mstrTitle;
    /** 提示信息标题下提示语文字 */
    private String mstrTitleTip;
    /** 左边按钮 */
    private Button mbtnLeft;
    /** 左边按钮文字 */
    private String mstrLeft;

    /** 右边按钮 */
    private Button mbtnRight;
    /** 右边按钮文字 */
    private String mstrRight;

    /** 按钮点击监听 */
    private OnCommonSendMsgClickListener mListener;

    /** 所加载的layout资源ID */
    private int mLayoutResID = 0;

    /**
     * ClassName:OnCommonSendMsgClickListener Function: 按钮点击监听接口 Reason: TODO ADD REASON
     * 
     * @author Administrator
     * @version CommonTipDlg
     * @since Ver 1.1
     * @Date 2014 2014-4-24 上午10:37:14
     * 
     * @see
     */
    public interface OnCommonSendMsgClickListener {
        /**
         * 左按钮点击处理
         * 
         * <br>
         * Created 2014-6-9 下午5:44:48
         * 
         * @author : linsj
         */
        void onLeftBtnClicked();

        /**
         * 右按钮点击处理
         * 
         * <br>
         * Created 2014-6-9 下午5:45:16
         * 
         * @author : linsj
         */
        void onRightBtnClicked();
    }

    /**
     * 设置监听
     * 
     * <br>
     * Created 2014-6-6 下午5:29:56
     * 
     * @param listener
     *            监听器
     * @author : linsj
     */
    public void setOnListener(OnCommonSendMsgClickListener listener) {
        this.mListener = listener;
    }

    /**
     * Creates a new instance of CommonTipDlg.
     * 
     * @param context
     *            　上下文
     * @param strTip
     *            　　提示信息
     * @param strLeftBtn
     *            　左边按钮文字
     * @param strRightBtn
     *            　右边按钮文字
     * @param Resid
     *            　　　对话框样式ID
     */
    public CommonSendMsgCodeDlg(Context context, String strTip, String strLeftBtn,
            String strRightBtn, int Resid) {
        super(context, Resid);
        mContext = context;
        mstrTip = strTip;
        mstrLeft = strLeftBtn;
        mstrRight = strRightBtn;
        mLayoutResID = R.layout.common_send_msg_tip_dlg;
    }

    /**
     * 设置提示语
     * 
     * <br>
     * Created 2014-11-11 下午3:30:51
     * 
     * @param strTip
     * @author linsj
     */
    public void setTip(String strTip) {
        mstrTip = strTip;
        if (null != mtvTip) {
            mtvTip.setText(mstrTip);
        }
    }

    /**
     *设置标题
     * <br>Created 2014-12-10 上午9:53:23
     * @param strTitle 标题
     * @author       gpy
    */
    public void setTiTle(String strTitle) {
        mstrTitle = strTitle;
        if (null != mtvTitle) {
            mtvTitle.setText(mstrTitle);
        }
    }
    /**
     *设置标题下提示语
     * <br>Created 2014-12-10 上午9:53:23
     * @param strTitle 标题提示语
     * @author       gpy
    */
    public void setTiTleTip(String strTitleTip) {
        mstrTitleTip = strTitleTip;
        if (null != mtvWillSendTo) {
            mtvWillSendTo.setText(mstrTitleTip);
        }
    }
    /**
     * 设置要加载的布局资源ID
     * 
     * <br>
     * Created 2014-8-1 下午2:12:33
     * 
     * @param layoutResID
     * @author : linsj
     */
    public void setLayoutResID(int layoutResID) {
        mLayoutResID = layoutResID;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(mLayoutResID);

        // 宽度设置为屏幕的7 / 8
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        final int SEVEN = 7;
        final int EIGHT = 8;
        // 竖屏时，取屏幕宽度
        if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            lp.width = (int) ( StarAppUtils.getInstance().readDisplayMetrics().x  * SEVEN / EIGHT);
        } else {
            // 横屏时，取屏幕高度
            lp.width = (int) ( StarAppUtils.getInstance().readDisplayMetrics().y * SEVEN / EIGHT);
        }
        getWindow().setAttributes(lp);

        mtvTitle = (TextView) findViewById(R.id.tv_title);
        if (!TextUtils.isEmpty(mstrTitle)) {
            mtvTitle.setText(mstrTitle);
        }
        mtvWillSendTo = (TextView) findViewById(R.id.tv_will_send_msg_to);
        if (!TextUtils.isEmpty(mstrTitleTip)) {
            mtvWillSendTo.setText(mstrTitleTip);
        }
        mtvTip = (TextView) findViewById(R.id.tv_mobile);
        mtvTip.setText(mstrTip);

        mbtnLeft = (Button) findViewById(R.id.btn_left);
        mbtnLeft.setText(mstrLeft);

        mbtnRight = (Button) findViewById(R.id.btn_Right);
        mbtnRight.setText(mstrRight);

        mbtnLeft.setOnClickListener(clickListener);
        mbtnRight.setOnClickListener(clickListener);

        setTextTypeface();
    }

    /**
     * 设置控件为兰亭序字体
     * 
     * <br>
     * Created 2014-8-13 下午7:30:25
     * 
     * @author linsj
     */
    private void setTextTypeface() {
        StarAppUtils.setViewTypeFace(mtvTip, false);
        StarAppUtils.setViewTypeFace(mtvTitle, true);
        StarAppUtils.setViewTypeFace(mtvWillSendTo, false);
        StarAppUtils.setViewTypeFace(mbtnLeft, false);
        StarAppUtils.setViewTypeFace(mbtnRight, false);
    }

    /**
     * 点击事件
     */
    protected Button.OnClickListener clickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (R.id.btn_left == id) {
                if (null != mListener) {
                    mListener.onLeftBtnClicked();
                }
                dismiss();
            } else if (R.id.btn_Right == id) {
                if (null != mListener) {
                    mListener.onRightBtnClicked();
                }
                dismiss();
            } else {
                dismiss();
            }
        }
    };
}
